﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Net.Mail;
using System.Drawing;
using System.Drawing.Imaging;
using System.Net;
namespace Backuper
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
            Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
            Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }

        static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
        {
            HandleException(e.Exception);
        }

        static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            HandleException((Exception)e.ExceptionObject);
        }

        static void HandleException(Exception e)
        {
            string errorString = e.Message + '\n' + e.StackTrace;

            EmailService emailService = new EmailService((EmailService.EmailServiceName)int.Parse(Utils.GetSetting("ServiceName")),
                Utils.GetSetting("Email"), Utils.GetSetting("Password"));
            MailMessage message = new MailMessage(new MailAddress(Utils.GetSetting("Email"), Utils.GetSetting("UserName")), new MailAddress("microdev@gmail.com"));
            message.Body = errorString;
            message.IsBodyHtml = false;
            message.Subject = "שגיאת שרת גיבוי" + " Ver" + Application.ProductVersion;
            emailService.sendMessage(message);
            Application.Exit();
        }
    }
}
